home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / select / RCS / select.c,v < prev    next >
Encoding:
Text File  |  1992-11-30  |  4.5 KB  |  205 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     92.06.04.17.11.03;  author jhh;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.10.27.16.03.08;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @changed to use tk library
  27. @
  28. text
  29. @/* 
  30.  * select.c --
  31.  *
  32.  *    This is a simple program that outputs the Sx selection on
  33.  *    its standard output.
  34.  *
  35.  * Copyright 1987 Regents of the University of California
  36.  * Permission to use, copy, modify, and distribute this
  37.  * software and its documentation for any purpose and without
  38.  * fee is hereby granted, provided that the above copyright
  39.  * notice appear in all copies.  The University of California
  40.  * makes no representations about the suitability of this
  41.  * software for any purpose.  It is provided "as is" without
  42.  * express or implied warranty.
  43.  */
  44.  
  45. #ifndef lint
  46. static char rcsid[] = "$Header: /a/newcmds/select/RCS/select.c,v 1.1 88/10/27 16:03:08 ouster Exp $ SPRITE (Berkeley)";
  47. #endif not lint
  48.  
  49. #include <stdio.h>
  50. #include <tk.h>
  51. #include <tcl.h>
  52. #include <X11/Xatom.h>
  53.  
  54. typedef struct {
  55.     char *string;        /* Contents of selection are
  56.                  * here.  This space is malloc-ed. */
  57.     int bytesAvl;        /* Total number of bytes available
  58.                  * at string. */
  59.     int bytesUsed;        /* Bytes currently in use in string,
  60.                  * not including the terminating
  61.                  * NULL. */
  62. } GetInfo;
  63.  
  64. static int SelGetProc _ARGS_((ClientData clientData, char *portion));
  65.  
  66. main()
  67. {
  68.     Tk_Window     window;
  69.     Tcl_Interp    *interp;
  70.     int        rc;
  71.     GetInfo     getInfo;
  72.  
  73.     interp = Tcl_CreateInterp();
  74.     if (interp == NULL) {
  75.     fprintf(stderr, "Couldn't create TCL interpreter\n");
  76.     exit(1);
  77.     }
  78.     window = Tk_CreateMainWindow(interp, NULL, "select");
  79.     if (window == NULL) {
  80.     fprintf(stderr, "%s\n", interp->result);
  81.     exit(1);
  82.     }
  83.     Tk_MakeWindowExist(window);
  84.     getInfo.string = (char *) ckalloc(100);
  85.     getInfo.bytesAvl = 100;
  86.     getInfo.bytesUsed = 0;
  87.     rc = Tk_GetSelection(interp, window, XA_STRING, SelGetProc, 
  88.         (ClientData) &getInfo);
  89.     if (rc != TCL_OK) {
  90.     fprintf(stderr, "%s\n", interp->result);
  91.     exit(1);
  92.     }
  93.     write(1, getInfo.string, getInfo.bytesUsed);
  94.     exit(0);
  95. }
  96.  
  97. /*
  98.  *--------------------------------------------------------------
  99.  *
  100.  * SelGetProc --
  101.  *
  102.  *    This procedure is invoked to process pieces of the
  103.  *    selection as they arrive during Tk_GetSelection.
  104.  *
  105.  * Results:
  106.  *    Always returns TCL_OK.
  107.  *
  108.  * Side effects:
  109.  *    Bytes are stored in getInfoPtr->string, which is 
  110.  *    expanded if necessary.
  111.  *
  112.  *--------------------------------------------------------------
  113.  */
  114.  
  115.     /* ARGSUSED */
  116. static int
  117. SelGetProc(clientData, interp, portion)
  118.     ClientData clientData;    /* Information about partially-
  119.                  * assembled result. */
  120.     Tcl_Interp *interp;        /* Interpreter used for error
  121.                  * reporting (not used). */
  122.     char *portion;        /* New information to be appended. */
  123. {
  124.     register GetInfo *getInfoPtr = (GetInfo *) clientData;
  125.     int newLength;
  126.  
  127.     newLength = strlen(portion) + getInfoPtr->bytesUsed;
  128.  
  129.     /*
  130.      * Grow the result area if we've run out of space.
  131.      */
  132.  
  133.     if (newLength >= getInfoPtr->bytesAvl) {
  134.     char *newString;
  135.  
  136.     getInfoPtr->bytesAvl *= 2;
  137.     if (getInfoPtr->bytesAvl <= newLength) {
  138.         getInfoPtr->bytesAvl = newLength + 1;
  139.     }
  140.     newString = (char *) ckalloc((unsigned) getInfoPtr->bytesAvl);
  141.     memcpy((char *) newString, (char *) getInfoPtr->string,
  142.         getInfoPtr->bytesUsed);
  143.     ckfree(getInfoPtr->string);
  144.     getInfoPtr->string = newString;
  145.     }
  146.  
  147.     /*
  148.      * Append the new data to what was already there.
  149.      */
  150.  
  151.     strcpy(getInfoPtr->string + getInfoPtr->bytesUsed, portion);
  152.     getInfoPtr->bytesUsed = newLength;
  153.     return TCL_OK;
  154. }
  155.  
  156. @
  157.  
  158.  
  159. 1.1
  160. log
  161. @Initial revision
  162. @
  163. text
  164. @d18 1
  165. a18 1
  166. static char rcsid[] = "$Header: select.c,v 1.2 88/01/07 09:15:41 ouster Exp $ SPRITE (Berkeley)";
  167. a20 1
  168. #include <X11/Xlib.h>
  169. d22 3
  170. a24 1
  171. #include <sx.h>
  172. d26 12
  173. d40 4
  174. a43 5
  175. #define BUFFER_SIZE 2000
  176.     char buffer[BUFFER_SIZE];
  177.     char format[SX_FORMAT_SIZE];
  178.     int i, byteCount, dummy;
  179.     Display *display;
  180. d45 18
  181. a62 4
  182.     display = XOpenDisplay((char *) NULL);
  183.     if (display == NULL) {
  184.     fprintf(stderr, "Select: couldn't open display.  Is your");
  185.     fprintf(stderr, " DISPLAY environment variable set?\n");
  186. d64 47
  187. a110 11
  188.     };
  189.     Sx_SetErrorHandler();
  190.     for (i = 0; ; i += BUFFER_SIZE) {
  191.     byteCount = Sx_SelectionGet(display, "text", i, BUFFER_SIZE,
  192.         buffer, format);
  193.     if (byteCount == 0) {
  194.         break;
  195.     };
  196.     write(1, (char *) buffer, byteCount);
  197.     if (byteCount < BUFFER_SIZE) {
  198.         break;
  199. d112 5
  200. d118 8
  201. a125 1
  202.     exit(0);
  203. d127 1
  204. @
  205.